<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <style>
        .img-area img {
            width: 100%;
            height: 100%;
            border-radius: 6px;
            display: block;
            border: 1px solid;
            object-fit: contain;
            box-sizing: border-box;
        }
        .img-area {
            width: 155px;
            height: 155px;
            margin: 10px;
            position: relative;
        }

        .img-area i {
            position: absolute;
            right: 5px;
            top: 2px;
            color: white;
            z-index: 1;
            cursor: pointer;
        }

        .img-area::after {
            content: "";
            position: absolute;
            height: 20px;
            background-color: rgba(0, 0, 0, 0.5);
            top: 0;
            right: 0;
            width: 100%;
            border-radius: 5px 5px 0 0;
        }
    </style>
</head>

<body>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>
            <a href="./select.html" class="layui-btn layui-btn-sm" id="back">返回</a>
            修改院线
        </legend>
    </fieldset>

    <form class="layui-form" action="" lay-filter="cinemaForm" onsubmit="return false">



        <div class="layui-form-item">
            <div class="layui-block">
                <label class="layui-form-label">院线名称</label>
                <div class="layui-input-block">
                    <input type="tel" name="name" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>



        <div class="layui-form-item">
            <div class="layui-block">
                <label class="layui-form-label">院线地址</label>
                <div class="layui-input-block">
                    <input type="text" name="addr" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-block">
                <label class="layui-form-label">图片</label>
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" id="btnUpload">
                        <i class="layui-icon">&#xe67c;</i>图片上传
                    </button>
                </div>
            </div>
            <div class="layui-block">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                    <div class="img-area">
                        <img src="" alt="">
                        <i class="layui-icon layui-icon-close"></i>
                    </div>
                </div>
            </div>
            <div class="layui-block">
                <div class="layui-input-block">
                    <input type="text" name="image" style="display:none" lay-verify="required" autocomplete="off" class="imageTxt layui-input">
                </div>
            </div>
        </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-block">
                <label class="layui-form-label">联系电话</label>
                <div class="layui-input-block">
                    <input type="text" name="tel" lay-verify="required|phone" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>



        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="cinema-edit">修改院线</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="reset">重置</button>
            </div>
        </div>


    </form>
    <script src="../../js/lodash.min.js"></script>
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.12.4.min.js"></script>

    <script>

        function query(url) {
            const parst = url.split("?");
            if (parst.length == 1) {
                return {};
            }
            const obj = {};
            parst[1].split("&").map(item => item.split("=")).forEach(([p, v]) => {
                obj[p] = decodeURIComponent(v);
            })
            return obj;
        }
        const cinemaObj = query(location.href);
        let id = cinemaObj.id;
        function updateCinema(cinemaid) {
            layui.use(['form'], async function () {
                const form = layui.form;

                const cinema = await $.getJSON("/api/Cinema/" + id);
                $(".img-area img").prop("src", cinema.data.image);
                form.val("cinemaForm", cinema.data);

                //监听提交
                form.on('submit(cinema-edit)', async function (data) {
                    await $.ajax({
                        url: "/api/Cinema/" + id,
                        method: "put",
                        data: data.field,
                        success: function (data) {
                            console.log(data);
                            layer.msg('修改院线成功', {
                                icon: 1,
                                time: 1000
                            }, function () {
                                location.href = "./select.html";
                            });
                        }
                    });
                });

            });
        }
        updateCinema(id);

        layui.use("upload", function () {
            let upload = layui.upload;

            let uploadInst = upload.render({
                elem: '#btnUpload',
                url: "/api/upload/",
                field: "imgfile",
                multiple: true,
                drag: true,
                done: function (res) {
                    $(".img-area img").prop("src", res.data[0]);
                    $(".imageTxt").val(res.data[0]);
                    $(".img-area").show();

                },
                error: function () {
                    console.log("文件错误哦");
                }
            })
        });

        $(".img-area i").click(function () {
            $(".imageTxt").val("");
            $(".img-area img").prop("src", "");
            $(".img-area").hide();
        })

        $("#back").click(() => {
            location.href = "./cinemaSelect.html";
        });

    </script>

</body>

</html>